package cn.iocoder.yudao.module.system.controller.admin.appupgradeinfo;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.appupgradeinfo.vo.AppUpgradeInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.appupgradeinfo.vo.AppUpgradeInfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.appupgradeinfo.vo.AppUpgradeInfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.appupgradeinfo.AppUpgradeInfoDO;
import cn.iocoder.yudao.module.system.service.appupgradeinfo.AppUpgradeInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - app升级信息")
@RestController
@RequestMapping("/system/app-upgrade-info")
@Validated
public class AppUpgradeInfoController {

    @Resource
    private AppUpgradeInfoService appUpgradeInfoService;

    @GetMapping("/get")
    @Operation(summary = "获得app升级信息")
    @PermitAll
    public CommonResult<AppUpgradeInfoRespVO> get() {
        AppUpgradeInfoDO appUpgradeInfo = appUpgradeInfoService.getAppUpgradeInfo();
        return success(BeanUtils.toBean(appUpgradeInfo, AppUpgradeInfoRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建app升级信息")
    @PreAuthorize("@ss.hasPermission('system:app-upgrade-info:create')")
    public CommonResult<Long> createAppUpgradeInfo(@Valid @RequestBody AppUpgradeInfoSaveReqVO createReqVO) {
        return success(appUpgradeInfoService.createAppUpgradeInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新app升级信息")
    @PreAuthorize("@ss.hasPermission('system:app-upgrade-info:update')")
    public CommonResult<Boolean> updateAppUpgradeInfo(@Valid @RequestBody AppUpgradeInfoSaveReqVO updateReqVO) {
        appUpgradeInfoService.updateAppUpgradeInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除app升级信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:app-upgrade-info:delete')")
    public CommonResult<Boolean> deleteAppUpgradeInfo(@RequestParam("id") Long id) {
        appUpgradeInfoService.deleteAppUpgradeInfo(id);
        return success(true);
    }

//    @GetMapping("/get")
//    @Operation(summary = "获得app升级信息")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('system:app-upgrade-info:query')")
//    public CommonResult<AppUpgradeInfoRespVO> getAppUpgradeInfo(@RequestParam("id") Long id) {
//        AppUpgradeInfoDO appUpgradeInfo = appUpgradeInfoService.getAppUpgradeInfo(id);
//        return success(BeanUtils.toBean(appUpgradeInfo, AppUpgradeInfoRespVO.class));
//    }

    @GetMapping("/page")
    @Operation(summary = "获得app升级信息分页")
    @PreAuthorize("@ss.hasPermission('system:app-upgrade-info:query')")
    public CommonResult<PageResult<AppUpgradeInfoRespVO>> getAppUpgradeInfoPage(@Valid AppUpgradeInfoPageReqVO pageReqVO) {
        PageResult<AppUpgradeInfoDO> pageResult = appUpgradeInfoService.getAppUpgradeInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppUpgradeInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出app升级信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:app-upgrade-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppUpgradeInfoExcel(@Valid AppUpgradeInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppUpgradeInfoDO> list = appUpgradeInfoService.getAppUpgradeInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "app升级信息.xls", "数据", AppUpgradeInfoRespVO.class,
                        BeanUtils.toBean(list, AppUpgradeInfoRespVO.class));
    }

}